package utils;

import java.util.List;

public class NameUtils {

    public static String getNumber(String name) {
        String [] arr = name.split("");
        int k=0;
        boolean flag = false;
        String numberStr = "0123456789";
        String result = "";
        for (int i = arr.length-1; i >=0; i--) {
            if(k==0||flag){
                if(numberStr.indexOf(arr[i])!=-1){
                    result += arr[i];
                    if(k==0){
                        flag=true;
                    }
                    k++;
                }else{
                    if(k!=0){
                        flag=false;
                    }
                }
            }
        }
        result = new StringBuilder(result).reverse().toString();
        return result;
    }

    public static String getMaterial(String name) {
        //如果 文件名存在@ 就 从@后面的文件名 开始判断
        if (name.indexOf("@") != -1) {
            name = name.substring(name.indexOf("@") + 1);
        }
        List<String> list = ReadAndWriteFile.readtFile("info/text/Material.txt", "utf8");
        if (list != null && list.size() > 0) {
            for (String s : list) {
                s = s.trim();
                if (s.length() > 0) {
                    if (name.indexOf(s) != -1) {
                        return s;
                    }
                }
            }
        }
        return "";

    }

    public static int getWeight(String name) {
        int weight = 0;

        if (name.indexOf("@") != -1) {
            name = name.substring(name.indexOf("@") + 1);
        }
        //设置克重
        if (name.toLowerCase().indexOf("g") != -1) {
            String temp = name.toLowerCase();
            temp = temp.substring(temp.indexOf("@") + 1, name.toLowerCase().indexOf("g"));
            String[] tempArr = temp.split("");
            boolean flag = false;
            int mi = 0;
            String tempWeight = "";
            for (int i = tempArr.length - 1; i > 0; i--) {
                String numstr = "0123456789";
                if (mi != 0 && flag || mi == 0) {
                    if (numstr.indexOf(tempArr[i]) != -1) {
                        if (mi == 0) {
                            flag = true;
                        }
                        mi++;
                        tempWeight += tempArr[i];
                    } else {
                        if (mi != 0) {
                            flag = false;
                        }
                    }
                }

            }
            tempWeight = new StringBuilder(tempWeight).reverse().toString();
            if(!"".equals(tempWeight)&&tempWeight.length()>0){
                return Integer.valueOf(tempWeight);
            }

        }
        return weight;
    }

    public static String getModel(String name) {
        String model = "";
        if (name.indexOf("@") != -1) {
            //设置型号
            model = name.substring(0, name.indexOf("@"));
        }
        return model;
    }
}
